// 配置路径Route
import 'package:flutter/material.dart';
import 'package:flutter3/routes/fluro/application.dart';
import 'package:flutter3/routes/fluro/route_handler.dart';
import 'package:fluro/fluro.dart';

import '../../views/other_page/not_found.dart';

class RouterManager {

  static String root = '/'; // 根目录
  static String setting = '/home'; // demo菜单主页
  static String webView = '/webView'; // 网页加载
  static String wxSharePay = '/tabBar'; // 测试model传参
  static String listView = "/listView"; //listView
  static String tabBar = "/tabBar";
  static String component = "/component";

  // 配置route
  static void configureRoutes(FluroRouter router) {
    router.notFoundHandler = Handler(
        handlerFunc: (BuildContext? context, Map<String, List<String>> params) {
          return NotFoundPage();
        });

    //注册路由并指向所对应的handler(Handler对应的是界面)
    router.define(root, handler: rootHandler); // 根目录
    router.define(webView, handler: webViewHandler); // 网页加载
    router.define(wxSharePay, handler: wxSharePayHandler); // 测试model传参
    router.define(listView, handler: ListViewHandler); // 列表加载
    router.define(tabBar, handler: TabBarDemoHandler);
    router.define(component, handler: BaseComponentDemoHander);
  }

  // 对参数进行encode，解决参数中有特殊字符，影响fluro路由匹配
  static Future navigateTo(BuildContext context, String path, {required Map<String, dynamic> params, TransitionType transition = TransitionType.native}) {
    String query =  "";
    int index = 0;
    for (var key in params.keys) {
      var value = Uri.encodeComponent(params[key]);
      if (index == 0) {
        query = "?";
      } else {
        query = query + "\&";
      }
      query += "$key=$value";
      index++;
    }
    print('我是navigateTo传递的参数：$query');

    path = path + query;
    return Application.router.navigateTo(context, path, transition:transition);
  }
}